import { createFloder, getList } from '../../api/personal'

const personal = {
  state: {
    floderList: []
  },
  mutations: {
    UPDATE_FloderList: (state, floder) => {
      state.floderList.push(floder)
    },
    SET_FloderList: (state, floderList) => {
      state.floderList = floderList
    }
  },
  actions: {
    // 新增个人文件夹
    CreateFloder(context, floderInfo, username) {
      const { floderName, imagePath, childrens, setTime, floderPath } = floderInfo
      const args = {
        floderName,
        imagePath,
        childrens,
        floderPath,
        setTime,
        // ***坑点
        username
      }
      return new Promise((resolve, reject) => {
        createFloder(args).then(response => {
          console.log('data:', response)
          const { msg } = response

          context.commit('UPDATE_FloderList', floderInfo)
          resolve(msg)
        }).catch(err => {
          reject(err)
        })
      })
    },
    // 获取个人所有文件夹
    GetFloderInfo(context, username) {
      return new Promise((resolve, reject) => {
        getList(username).then(response => {
          const { floderList } = response
          const { floders } = floderList
          // 更新状态
          context.commit('SET_FloderList', floders)
          resolve(floders)
        }).catch(err => {
          reject(err)
        })
      })
    }
  }
}

export default personal
